//
//  ViewController.m
//  自定义tabbar
//
//  Created by Admin on 2017/3/4.
//  Copyright © 2017年 Admin. All rights reserved.
//

#define ColorA(r, g, b, a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:(a)/255.0]
#define Color(r, g, b) ColorA((r), (g), (b), 255)
#define RandomColor Color(arc4random_uniform(255), arc4random_uniform(255), arc4random_uniform(255))

#import "ViewController.h"
#import "tabbar.h"
#import "FirstViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupItemTitleTextAttributes];
    [self setupChildViewControllers];
    [self setupTabbar];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)setupItemTitleTextAttributes {
    UITabBarItem *item = [UITabBarItem appearance];
    // 普通状态下的文字属性
    NSMutableDictionary *normalAttrs = [NSMutableDictionary dictionary];
    normalAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:14];
    normalAttrs[NSForegroundColorAttributeName] = [UIColor grayColor];
    [item setTitleTextAttributes:normalAttrs forState:UIControlStateNormal];
    // 选中状态下的文字属性
    NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
    selectedAttrs[NSForegroundColorAttributeName] = [UIColor darkGrayColor];
    [item setTitleTextAttributes:normalAttrs forState:UIControlStateSelected];
}

- (void)setupChildViewControllers {
    [self setupOneChildViewController:[[UINavigationController alloc] initWithRootViewController:[[FirstViewController alloc] init]] title:@"精华" image:@"tabBar_essence_icon" selectedImage:@"tabBar_essence_click_icon"];
    [self setupOneChildViewController:[[UINavigationController alloc] initWithRootViewController:[[UIViewController alloc] init]] title:@"新帖" image:@"tabBar_new_icon" selectedImage:@"tabBar_new_click_icon"];
    [self setupOneChildViewController:[[UINavigationController alloc] initWithRootViewController:[[UIViewController alloc] init]] title:@"关注" image:@"tabBar_friendTrends_icon" selectedImage:@"tabBar_friendTrends_click_icon"];
}

- (void)setupOneChildViewController:(UIViewController *)vc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage {
    vc.view.backgroundColor = [UIColor whiteColor];
    vc.tabBarItem.title = title;
    if (image.length) {
        vc.tabBarItem.image = [UIImage imageNamed:image];
        vc.tabBarItem.selectedImage = [UIImage imageNamed:selectedImage];
    }
    [self addChildViewController:vc];
}

- (void)setupTabbar {
    tabbar *tab = [[tabbar alloc] init];
    tab.itemPositioning = UITabBarItemPositioningFill;
    [self setValue:tab forKeyPath:@"tabBar"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
